<?php
/*
Plugin Name: dKret Links widget
Description: Adds a sidebar widget to display the Linkroll.
Author: J. Kretzschmar
Version: 1.1
Author URI: http://diekretzschmars.de
*/

function widget_dkret_blogroll($args) {
	extract($args);
	$options = get_option('widget_dkret_links');
	$title = empty($options['title']) ? __('Blogroll','dkret') : $options['title'];
	if ($options['linkcat']) {
		echo "$before_widget";
		echo "<ul>";
		wp_list_bookmarks();
		echo "</ul>";
		echo "$after_widget";
	} else {
		echo "$before_widget";
		echo "$before_title".$title.$after_title;
		echo "<ul>";
		wp_list_bookmarks('categorize=0&orderby=rand&title_li=&limit=5');
		echo "</ul>";
		echo "$after_widget";
  }
}

// Admin section
function widget_control_dkret_blogroll() {
 	global $wpdb;
 	$options = get_option('widget_dkret_links');
    		
	if ( $_POST['dkret-submit'] ) {

		$options['title'] = strip_tags(stripslashes($_POST['dkret-blogroll-title']));
		$options['linkcat'] = isset($_POST['dkret-blogroll-linkcat']);
		update_option('widget_dkret_links', $options);
	}
	
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
	$linkcat = $options['linkcat'] ? 'checked="checked"' : '';
	
	// The Box content
	echo '<p style="text-align:right;"><label for="dkret-blogroll-title" />' . __('Title:') . ' <input style="width: 200px;" id="dkret-blogroll-title" name="dkret-blogroll-title" type="text" value="'.$title.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-bogroll-linkcat">' . __('Show Categories in Blogroll:', 'dkret') . ' <input id="dkret-blogroll-linkcat" name="dkret-blogroll-linkcat" type="checkbox" '. $linkcat .'  /></label></p>';
	echo '<input type="hidden" id="dkret-submit" name="dkret-submit" value="1" />';
}

 	$options = get_option('widget_dkret_links');
 	if ( !is_array($options) )
		$options = array('linkcat'=>'checked="checked"',);
		update_option('widget_dkret_links', $options);

$widget_ops = array('classname' => 'widget_dkret_links', 'description' => __( "Your blogroll (dkret)", 'dkret') );
wp_register_sidebar_widget('links', __('Links'), 'widget_dkret_blogroll', $widget_ops);
unregister_widget_control('links');
wp_register_widget_control('links', __('Links'), 'widget_control_dkret_blogroll');
?>
